package com.yalu.service.data.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yalu.dao.data.StoresDao;
import com.yalu.entity.base.BaseEntity;
import com.yalu.entity.data.Stores;
import com.yalu.service.base.impl.BaseServiceImpl;
import com.yalu.service.data.StoresService;
import com.yalu.util.BaseUtil;

@Service
public class StoresServiceImpl extends BaseServiceImpl<Stores, Integer>
		implements StoresService {

	@Resource
	private StoresDao storesDao;

	@Resource
	public void setBaseDao(StoresDao storesDao) {
		super.setBaseDao(storesDao);
	}

	@Override
	public int[] save(List<BaseEntity> entityList) {
		int[] returnCounts = new int[2];
		int saveCount = 0;
		int updateCount = 0;
		String time = BaseUtil.getTime();
		for(BaseEntity baseEntity : entityList){
			Stores entity = (Stores)baseEntity;
			Integer key  = entity.getNo();
			Stores temp = storesDao.get(key);
			if(temp==null){
				entity.setCreatedt(time);
				storesDao.save(entity);
				saveCount++;
			}else{
				entity.setUpdatedt(time);
				BeanUtils.copyProperties(entity,temp, new String[] { "createdt",
						"createby", "updateby" });
				storesDao.update(temp);
				updateCount++;
			}
		}
		returnCounts[0]=saveCount;
		returnCounts[1]=updateCount;
		return returnCounts;
	}


}
